/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
package com.dci.controller.project;

import com.alibaba.fastjson.JSONObject;
import com.dci.context.Config;
import com.dci.model.project.*;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.project.*;
import com.dci.util.UUIDBuild;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.project
 * @Description: 项目信息修改记录
 * @author: 李振振
 * @date: 2019/7/11
 */
@Controller
@RequestMapping("/projectLog")
public class ProjectLogController {

    @Resource
    private ProjectLogService projectLogService;

    @Value("${upload.path}")
    private String path;
    @Value("${upload.url}")
    private String url;
    @Value("${upload.urlP}")
    private String urlP;
    /**
     * 修改 项目信息 并新增记录
     *
     * @param projectManager
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/projectLog")
    @ResponseBody
    @ApiOperation(value = "修改 项目信息 并新增修改记录")
    public ResultInfo updateProjectInfo(@RequestBody ProjectManager projectManager) {
        try {
            return projectLogService.updateProject(projectManager);
        } catch (Exception e) {
            return ResultUtil.createFail(null,911,null);
        }
    }

    /**
     * 修改提交表单和上传文件 并记录
     *
     * @param
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/updateProject")
    @ResponseBody
    @ApiOperation(value = "修改提交表单和上传文件 并记录")
    public  ResultInfo approvalAndSave(@RequestParam("assessFile") MultipartFile[] assessFile, @RequestParam("designFile") MultipartFile[] designFile,
                                       @RequestParam("constructFile") MultipartFile[] constructFile,@RequestParam("completedFile") MultipartFile[] completedFile,
                                       @RequestParam("locationFile") MultipartFile[] locationFile,@RequestParam("formData") String formData) {
        JSONObject jsonObject = JSONObject.parseObject(formData);
        ProjectManager projectManager = JSONObject.toJavaObject(jsonObject, ProjectManager.class);

        if((assessFile != null && assessFile.length > 0) || (designFile != null && designFile.length > 0)
                || (constructFile != null && constructFile.length > 0) || (completedFile != null && completedFile.length > 0)
                || (locationFile != null && locationFile.length > 0)){

        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyymmddHHMMss");
        String address = "";
            String fileNames = "";
            String videoFileNames = "";
            String constructionFileNames = "";
            String completionFileNames = "";
            String locationFileNames = "";
            String filePath = "";
            String urlPath = "";
            if(!StringUtils.isEmpty(projectManager.getFileNamesQuo())){
                fileNames = projectManager.getFileNamesQuo();
            }
            if(!StringUtils.isEmpty(projectManager.getFileNamesConcept())){
                videoFileNames = projectManager.getFileNamesConcept();
            }
        if(StringUtils.isEmpty(projectManager.getFileAddress())){
            address = df.format(now);
            filePath = path + File.separator + address + "/";
            urlPath = url + "/" + address + "/";

        } else
        {
            String[] urlList =  projectManager.getFileAddress().split("/");
            address = urlList[urlList.length-1];
            filePath = path + File.separator + address + "/";
            urlPath = projectManager.getFileAddress();
        }
        File fileDir = new File(filePath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        if (assessFile.length != 0) {
            for (MultipartFile file : assessFile) {
                String fileName = file.getOriginalFilename();
                if (fileNames == "") {
                    fileNames += fileName;
                } else {
                    fileNames += ',' + fileName;
                }
                try {
                    file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                } catch (IOException e) {
                    continue;
                }
            }
        }

        if (designFile.length != 0) {
            for (MultipartFile file : designFile) {
                String fileName = file.getOriginalFilename();
                if (videoFileNames == "") {
                    videoFileNames += fileName;
                } else {
                    videoFileNames += ',' + fileName;
                }
                try {
                    file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                } catch (IOException e) {
                    continue;
                }
            }
        }

            if (constructFile.length != 0) {
                for (MultipartFile file : constructFile) {
                    String fileName = file.getOriginalFilename();
                    if (constructionFileNames == "") {
                        constructionFileNames += fileName;
                    } else {
                        constructionFileNames += ',' + fileName;
                    }
                    try {
                        file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                    } catch (IOException e) {
                        continue;
                    }
                }
            }

            if (completedFile.length != 0) {
                for (MultipartFile file : completedFile) {
                    String fileName = file.getOriginalFilename();
                    if (completionFileNames == "") {
                        completionFileNames += fileName;
                    } else {
                        completionFileNames += ',' + fileName;
                    }
                    try {
                        file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                    } catch (IOException e) {
                        continue;
                    }
                }
            }

            if (locationFile.length != 0) {
                for (MultipartFile file : locationFile) {
                    String fileName = file.getOriginalFilename();
                    if (locationFileNames == "") {
                        locationFileNames += fileName;
                    } else {
                        locationFileNames += ',' + fileName;
                    }
                    try {
                        file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                    } catch (IOException e) {
                        continue;
                    }
                }
            }
            if(StringUtils.isEmpty(projectManager.getFileAddress())){
                projectManager.setFileAddress(urlPath);
            }

       /* if(StringUtils.isEmpty(projectManager.getFileNamesQuo())){
            projectManager.setFileNamesQuo(fileNames);
        }
        if(StringUtils.isEmpty(projectManager.getFileNamesConcept())){
            projectManager.setFileNamesConcept(videoFileNames);
        }*/
        }

        try {
            projectManager.setOperate("edit");
            return projectLogService.updateProject(projectManager);
        } catch (Exception e) {
            return ResultUtil.createFail(null,911,null);
        }
    }

    /**
     * 根据条件查询 修改记录
     *
     * @param record 记录信息
     * @return PageResult 封装返回值
     */
    @PostMapping("/queryProjectLogList")
    @ResponseBody
    @ApiOperation(value = "根据条件查询 修改记录信息")
    public PageResult queryProjectLogList(@RequestBody ProjectLog record) {
        try {
            return projectLogService.queryProjectLogList(record);
        } catch (Exception e) {
            return PageResult.success(null,null,"911",(long)0 );
        }
    }

    /**
     * 根据条件分页查询 修改记录
     *
     * @param record 记录信息
     * @param pageNum 页数
     * @param pageSize 每页总数
     * @return PageResult 封装返回值
     */
    @PostMapping("/queryProjectLogPageList/{pageNum}/{pageSize}")
    @ResponseBody
    @ApiOperation(value = "根据条件分页查询 修改记录信息")
    public PageResult queryProjectLogPageList(@RequestBody ProjectLog record , @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            return projectLogService.queryProjectLogPageList(record,pageNum,pageSize);
        } catch (Exception e) {
            return PageResult.success(null,null,"911",(long)0 );
        }
    }

    /**
     * 根据主键批量删除记录信息接口
     * @param object id信息
     * @return ResultInfo 封装数据 成功msg:906 失败msg:911
     */
    @DeleteMapping("/deleteByPrimaryKey")
    @ResponseBody
    @ApiOperation("删除记录信息接口")
    public ResultInfo deleteByPrimaryKey(@RequestBody String object) {
        return projectLogService.deleteByPrimaryKey( object );
    }

    /**
     * 更新记录接口
     * @param record
     * @return ResultInfo 封装数据 成功msg:906 失败msg:911
     */
    @PostMapping("/updateProjectLog")
    @ResponseBody
    @ApiOperation("更新修改记录接口")
    public ResultInfo updateProjectLog(@RequestBody ProjectLog record) {
        return projectLogService.updateProjectLog(record);
    }

    /**
     * 新增记录接口
     * @param record
     * @return ResultInfo 封装数据 成功msg:906 失败msg:911
     */
    @PostMapping("/insertSelective")
    @ResponseBody
    @ApiOperation("新增记录接口")
    public JsonData insertSelective(@RequestBody ProjectLog record) {
        return projectLogService.insertSelective(record);
    }
}
